{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "import torchvision\n",
    "import torchvision.models as models\n",
    "import torchvision.transforms as transforms\n",
    "from torchvision.transforms import RandAugment\n",
    "from torch.optim import lr_scheduler\n",
    "import numpy as np\n",
    "import time\n",
    "import copy\n",
    "import matplotlib.pyplot as plt\n",
    "from CcNet import *\n",
    "from utils import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda:0\n",
      "Files already downloaded and verified\n",
      "Files already downloaded and verified\n"
     ]
    }
   ],
   "source": [
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(device)\n",
    " ## 修改模型\n",
    "net = CcNet().to(device)\n",
    "num_epochs=50\n",
    " # 定义损失函数和优化器\n",
    "trainloader,testloader = get_data_loader()\n",
    "criterion = nn.CrossEntropyLoss()\n",
    "optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-4)\n",
    "scheduler = lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.1)\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 0/49 轮训练\n",
      "----------\n",
      "train 损失: 3.9648 Top-1 准确率: 0.0969 Top-5 准确率: 0.2865\n",
      "val 损失: 4.2089 Top-1 准确率: 0.0810 Top-5 准确率: 0.2844\n",
      "\n",
      "第 1/49 轮训练\n",
      "----------\n",
      "train 损失: 3.1935 Top-1 准确率: 0.2142 Top-5 准确率: 0.5023\n",
      "val 损失: 3.4346 Top-1 准确率: 0.1942 Top-5 准确率: 0.4973\n",
      "\n",
      "第 2/49 轮训练\n",
      "----------\n",
      "train 损失: 2.7052 Top-1 准确率: 0.3043 Top-5 准确率: 0.6256\n",
      "val 损失: 3.7217 Top-1 准确率: 0.2152 Top-5 准确率: 0.5012\n",
      "\n",
      "第 3/49 轮训练\n",
      "----------\n",
      "train 损失: 2.3647 Top-1 准确率: 0.3726 Top-5 准确率: 0.7035\n",
      "val 损失: 2.7794 Top-1 准确率: 0.3127 Top-5 准确率: 0.6309\n",
      "\n",
      "第 4/49 轮训练\n",
      "----------\n",
      "train 损失: 2.1494 Top-1 准确率: 0.4256 Top-5 准确率: 0.7458\n",
      "val 损失: 2.0784 Top-1 准确率: 0.4440 Top-5 准确率: 0.7612\n",
      "\n",
      "第 5/49 轮训练\n",
      "----------\n",
      "train 损失: 1.9620 Top-1 准确率: 0.4681 Top-5 准确率: 0.7843\n",
      "val 损失: 2.6230 Top-1 准确率: 0.3614 Top-5 准确率: 0.6808\n",
      "\n",
      "第 6/49 轮训练\n",
      "----------\n",
      "train 损失: 1.8239 Top-1 准确率: 0.4994 Top-5 准确率: 0.8095\n",
      "val 损失: 2.1991 Top-1 准确率: 0.4286 Top-5 准确率: 0.7562\n",
      "\n",
      "第 7/49 轮训练\n",
      "----------\n",
      "train 损失: 1.7243 Top-1 准确率: 0.5234 Top-5 准确率: 0.8281\n",
      "val 损失: 2.0403 Top-1 准确率: 0.4681 Top-5 准确率: 0.7685\n",
      "\n",
      "第 8/49 轮训练\n",
      "----------\n"
     ]
    }
   ],
   "source": [
    "model,(train_losses, train_top1_accs, train_top5_accs, val_losses, val_top1_accs, val_top5_accs) = train_model(net,trainloader,device,testloader, criterion, optimizer, scheduler, num_epochs)  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def to_cpu_numpy(tensor):\n",
    "    return tensor.cpu().numpy()\n",
    "def to_cpu(list_):\n",
    "    if isinstance(list_, list):\n",
    "        return list(map(to_cpu_numpy,list_))\n",
    "    else:\n",
    "        return to_cpu_numpy(list_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'train_losses' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m plot_training_results(\u001b[43mtrain_losses\u001b[49m, to_cpu(train_top1_accs), to_cpu(train_top5_accs), val_losses, to_cpu(val_top1_accs), to_cpu(val_top5_accs))\n",
      "\u001b[0;31mNameError\u001b[0m: name 'train_losses' is not defined"
     ]
    }
   ],
   "source": [
    "plot_training_results(train_losses, to_cpu(train_top1_accs), to_cpu(train_top5_accs), val_losses, to_cpu(val_top1_accs), to_cpu(val_top5_accs))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
